package com.ebanca.drawme.util;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import android.content.Context;

/**
 * Utility class that provides methods useful for serializing/deserializing objects.
 */

public class SerializationUtil {

    /**
     * The method serializes object into a file.
     *
     * @param fileName
     * @param object
     *
     * @throws Exception
     */
    public static void serializeToFile(Context context, String fileName, Object object) throws Exception {
    	// for debug purposes the mode is MODE_WORLD_READABLE
    	FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_WORLD_READABLE);
        ObjectOutputStream ous = new ObjectOutputStream(fos);

        ous.writeObject(object);
        ous.flush();
        ous.close();
    }

    /**
     * The method deserializes back an object from a file.
     *
     * @param fileName
     *
     * @return
     *
     * @throws Exception
     */
    public static Object deserializeFromFile(Context context, String fileName) throws Exception {
    	FileInputStream fis = context.openFileInput(fileName);
        ObjectInputStream ois = new ObjectInputStream(fis);

        return ois.readObject();
    }
}
